package request

import (
	"cloud-navigation/app/common/response"
	"strconv"

	"github.com/gin-gonic/gin"
)

func GetPages(c *gin.Context) (page, limit uint64, err response.Validator) {
	page, err1 := strconv.ParseUint(c.DefaultQuery("page", "1"), 10, 64)
	if err1 != nil {
		err := response.Validator{
			Field:   "page",
			Message: "页码错误",
		}
		return 0, 0, err
	}
	limit, err2 := strconv.ParseUint(c.DefaultQuery("limit", "10"), 10, 64)
	if err2 != nil {
		err := response.Validator{
			Field:   "limit",
			Message: "每页条数错误",
		}
		return 0, 0, err
	}
	return
}

func GetNavQuery(c *gin.Context) (name, url string, assort int, validator response.Validator) {
	name = c.DefaultQuery("name", "")
	url = c.DefaultQuery("url", "")
	if len(name) >= 20 {
		validator = response.Validator{
			Field:   "name",
			Message: "字符超出长度",
		}
		return
	}
	if len(url) >= 200 {
		validator = response.Validator{
			Field:   "url",
			Message: "字符超出长度",
		}
		return
	}
	assort, err := strconv.Atoi(c.DefaultQuery("assort_id", "0"))
	if err != nil {
		validator = response.Validator{
			Field:   "assort_id",
			Message: "分类ID错误",
		}
		return
	}
	return
}
